package com.jerry.walking.module.activityMe.fragment;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;

import com.jerry.walking.R;
import com.jerry.walking.base.BaseFragment;
import com.jerry.walking.module.activityMe.activity.SettingActivity;
import com.jerry.walking.module.home.activity.MainActivity;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.OnClick;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.PlatformDb;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.tencent.qq.QQ;

/**
 * Created by Yang on 2016/5/9.
 *
 *          我的主界面
 *
 *
 */
public class ActivityMeFragment extends BaseFragment implements PlatformActionListener{

    @BindView(R.id.iv_me_setting)
    ImageView imageView;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_me_activity;
    }

    @OnClick(R.id.tv_me_qq)
    void sinaLogin(){
        // 获取到对应平台
        Platform qqPlatform = ShareSDK.getPlatform(QQ.NAME);

        // 是否已经鉴权
        if (qqPlatform.isAuthValid())
        {
            // 内部使用SharePreference保存用户信息
            PlatformDb platformDb = qqPlatform.getDb();

            // 如果用户ID存在，则认为用户是合法用户，允许进入系统
            if (platformDb.isValid() && platformDb.getUserId() != null)
            {
                Intent intent = new Intent();
                intent.setClass(getContext(), MainActivity.class);
                startActivity(intent);

            }
            return;
        }

        qqPlatform.SSOSetting(false);

        qqPlatform.setPlatformActionListener(this);

        qqPlatform.authorize();
    }
    @Override
    protected void initViews() {
        super.initViews();
        ShareSDK.initSDK(getContext());

        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ActivityMeFragment.this.getActivity(), SettingActivity.class);

                ActivityMeFragment.this.getActivity().startActivity(intent);
            }
        });

    }
//    private void authorize(Platform plat) {
//        if (plat == null) {
//            return;
//        }
//        //判断指定平台是否已经完成授权
//        if(plat.isAuthValid()) {
//            String userId = plat.getDb().getUserId();
//            if (userId != null) {
//                UIHandler.sendEmptyMessage(0, this);
//                login(plat.getName(), userId, null);
//                return;
//            }
//        }
//        plat.setPlatformActionListener(this);
//        // true不使用SSO授权，false使用SSO授权
//        plat.SSOSetting(true);
//        //获取用户资料
//        plat.showUser(null);
//    }

    @Override
    protected void loadData() {
        super.loadData();
    }

    @Override
    public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap)
    {
        // 如果onComplete()方法被回调，表示授权成功，引导用户进入系统
        if (platform.isValid() && platform.getDb().getUserId() != null)
        {
            Intent intent = new Intent();
            intent.setClass(getContext(), MainActivity.class);
            startActivity(intent);

        }
    }


    @Override
    public void onError(Platform platform, int i, Throwable throwable){
    }

    @Override
    public void onCancel(Platform platform, int i){
    }
}